namespace :fake_data do
  task :insert => :environment do
    harvey = Instructor.new(:first_name=>"Brian",:last_name=>"Harvey",:url=>"http://cs.berkeley.edu/~bh")
    if harvey.save
      puts "Brian Harvey Created Successfully"
    else
      harvey = Instructor.find(:first,:conditions=>{:first_name=>'Brian',:last_name=>"Harvey"})
      raise "Harvey not found" unless harvey
    end
    
    hilfinger = Instructor.new(:first_name=>"Paul",:last_name=>"Hilfinger",:url=>"http://cs.berkeley.edu/~hilfingr")
    if hilfinger.save
      puts "Paul Hilfinger Created Successfully"
    else
      hilfinger = Instructor.find(:first,:conditions=>{:first_name=>'Paul',:last_name=>"Hilfinger"})
      raise "Hilfinger not found" unless hilfinger
    end
    
    fa09 = Term.new(:start_date=>DateTime.new(y=2009,m=8,d=24),:end_date=>DateTime.new(y=2009,m=12,d=19),:name=>"Fall 2009")
    if fa09.save
      puts "Fall 09 created successfully"
    else
      fa09 = Term.find(:first,:conditions=>{:name=>"Fall 2009"})
      raise "Fall 09 not found" unless fa09
    end
    
    cs61A = Course.find(:first,:conditions=>{:department=>"CS",:course_number=>"61A",:term_id=>fa09})
    if not cs61A
      cs61A = Course.new(:department=>"CS",:course_number=>"61A",:term=>fa09,:description=>"Introduction to computer science",:units=>4)
      cs61A.save!
      puts "CS61A created successfully"
    end
    
    cs61B = Course.find(:first,:conditions=>{:department=>"CS",:course_number=>"61B",:term_id=>fa09})
    if not cs61B
      cs61B = Course.new(:department=>"CS",:course_number=>"61B",:term=>fa09,:description=>"Introduction to computer science",:units=>4)
      cs61B.save!
      puts "CS61B created successfully"
    end
    
    cs61a_lecture_args = {
      :event_name=>"CS61a lecture",
      :event_type=>"LEC",
      :location=>"1 PIMENTEL",
      :start_time=>Time.parse("14:00"),
      :stop_time=>Time.parse("15:00"),
      :monday=>true,
      :tuesday=>false,
      :wednesday=>true,
      :thursday=>false,
      :friday=>true,
      :saturday=>false,
      :sunday=>false,
      :finals_group=>15,
      :ccn=>26266,
      :section_number=>"001",
      :course=>cs61A
    }
    cs61Alec = Lecture.new(cs61a_lecture_args)
    if cs61Alec.save
      cs61Alec.instructors << harvey
      puts "CS61A lecture created successfully"
    else
      cs61Alec = Lecture.find(:first,:conditions=>{:course_id=>cs61A})
      raise "CS61A lecture not found" unless cs61Alec
    end
    cs61a_disc_args = {
      :event_name=>"CS61a discussion",
      :event_type=>"DIS",
      :location=>"310 SODA",
      :start_time=>Time.parse("15:30"),
      :stop_time=>Time.parse("17:00"),
      :monday=>false,
      :tuesday=>false,
      :wednesday=>true,
      :thursday=>false,
      :friday=>false,
      :saturday=>false,
      :sunday=>false,
      :ccn=>26272,
      :section_number=>"011",
      :course=>cs61A
    }
    cs61a_disc_args2 = cs61a_disc_args.clone
    cs61a_disc_args2[:start_time] = Time.parse("17:00")
    cs61a_disc_args2[:stop_time] = Time.parse("18:30")
    cs61a_disc_args2[:ccn] = 26278
    cs61a_disc_args2[:section_number] = "012"
    cs61Adisc1 = Discussion.new(cs61a_disc_args)
    cs61Adisc2 = Discussion.new(cs61a_disc_args2)
    cs61Adisc1.save
    cs61Adisc2.save
    cs61a_lab_args = {
      :event_name=>"CS61a lab",
      :event_type=>"LAB",
      :location=>"271 SODA",
      :start_time=>Time.parse("15:30"),
      :stop_time=>Time.parse("17:00"),
      :monday=>true,
      :tuesday=>false,
      :wednesday=>false,
      :thursday=>false,
      :friday=>false,
      :saturday=>false,
      :sunday=>false,
      :ccn=>26269,
      :section_number=>"111",
      :course=>cs61A
    }
    cs61a_lab_args2 = cs61a_lab_args.clone
    cs61a_lab_args2[:start_time] = Time.parse("17:00")
    cs61a_lab_args2[:stop_time] = Time.parse("18:30")
    cs61a_lab_args2[:ccn] = 26275
    cs61a_lab_args2[:section_number] = "112"
    cs61Alab1 = Lab.create(cs61a_lab_args)
    cs61Alab2 = Lab.create(cs61a_lab_args2)
    
    
    cs61b_lecture_args = {
      :event_name=>"CS61B lecture",
      :event_type=>"LEC",
      :location=>"2050 VALLEY LSB",
      :start_time=>Time.parse("13:00"),
      :stop_time=>Time.parse("14:00"),
      :monday=>true,
      :tuesday=>false,
      :wednesday=>true,
      :thursday=>false,
      :friday=>true,
      :saturday=>false,
      :sunday=>false,
      :finals_group=>12,
      :ccn=>26341,
      :section_number=>"001",
      :course=>cs61B
    }
    cs61Blec = Lecture.new(cs61b_lecture_args)
    if cs61Blec.save
      cs61Blec.instructors << hilfinger
      puts "CS61B lecture created successfully"
    else
      cs61Blec = Lecture.find(:first,:conditions=>{:course_id=>cs61B})
      raise "CS61B lecture not found" unless cs61Blec
    end
    cs61b_disc_args = {
      :event_name=>"CS61b discussion",
      :event_type=>"DIS",
      :location=>"6 EVANS",
      :start_time=>Time.parse("10:00"),
      :stop_time=>Time.parse("11:00"),
      :monday=>false,
      :tuesday=>false,
      :wednesday=>false,
      :thursday=>true,
      :friday=>false,
      :saturday=>false,
      :sunday=>false,
      :ccn=>26347,
      :section_number=>"011",
      :course=>cs61B
    }
    cs61b_disc_args2 = cs61b_disc_args.clone
    cs61b_disc_args2[:start_time] = Time.parse("11:00")
    cs61b_disc_args2[:stop_time] = Time.parse("12:00")
    cs61b_disc_args2[:ccn] = 26353
    cs61b_disc_args2[:section_number] = "012"
    cs61Bdisc1 = Discussion.new(cs61b_disc_args)
    cs61Bdisc2 = Discussion.new(cs61b_disc_args2)
    cs61Bdisc1.save
    cs61Bdisc2.save
    cs61b_lab_args = {
      :event_name=>"CS61b lab",
      :event_type=>"LAB",
      :location=>"271 SODA",
      :start_time=>Time.parse("09:00"),
      :stop_time=>Time.parse("11:00"),
      :monday=>false,
      :tuesday=>true,
      :wednesday=>false,
      :thursday=>false,
      :friday=>false,
      :saturday=>false,
      :sunday=>false,
      :ccn=>26344,
      :section_number=>"111",
      :course=>cs61B
    }
    cs61b_lab_args2 = cs61b_lab_args.clone
    cs61b_lab_args2[:start_time] = Time.parse("11:00")
    cs61b_lab_args2[:stop_time] = Time.parse("13:00")
    cs61b_lab_args2[:ccn] = 26350
    cs61Blab1 = Lab.create(cs61b_lab_args)
    cs61Blab2 = Lab.create(cs61b_lab_args2)
    
    dummyUser = User.new(:login=>"dummy",:email=>"dummy@dum.edu",:password=>"dummy1",:password_confirmation=>"dummy1")
    if dummyUser.save
      puts "dummy user created successfully"
    else
      dummyUser = User.find(:first,:conditions=>{:login=>"dummy"})
      raise "Dummy user not found" unless dummyUser
    end
    partyArgs = {
      :event_name => "Party",
      :event_type => "Commitment",
      :location   => "McMurphy's house of fish",
      :start_time => Time.parse("21:00"),
      :stop_time => Time.parse("23:59"),
      :monday   => false,
      :tuesday  => false,
      :wednesday => false,
      :thursday  => false,
      :friday    => false,
      :saturday => true,
      :sunday   => true,
      :repeating => true,
      :user => dummyUser
    }
    party = Commitment.find(:first,:conditions=>{:user_id=>dummyUser,:location=>"McMurphy's house of fish"})
    if not party
      party = Commitment.create!(partyArgs)
      puts "party created successfully"
    end
    schedule = Schedule.find(:first,:conditions=>{:user_id=>dummyUser})
    if not schedule
      schedule = Schedule.create!(:user=>dummyUser,:id=>1)
      puts "schedule created successfully"
    end
    if not schedule.events.include? party
      schedule.events << party
    end
    if not schedule.events.include? cs61Alec
      schedule.events << cs61Alec
    end
    if not schedule.events.include? cs61Blec
      schedule.events << cs61Blec
    end
  end
end